#coding:utf-8
from .import main
from ..import db,model
from ..model import Courses,Kinds,Videos,Teachers
from flask import render_template,request,url_for,redirect,session
import datetime,random
@main.route('/index')
#路由，装饰器的不是app，而是main
def index():

    #session任何py文件中可以执行
    
    return render_template('course.html')

@main.route('/add')
def add():
    # for item in range(1,8):
    #     name='teacher_%s' %item
    #     abstract='abstract' *random.randint(1,6)
    #     level=random.randint(0,1)
    #     if level==1:
    #         level='讲师'
    #     else:
    #         level='助教'
    #     teacher=Teachers(name=name,abstract=abstract,level=level)
    #     db.session.add(teacher)
    # db.session.commit()

    # for item in range(1,10):
    #     name='item_%s' %item
        
    #     kind=Kinds(name=name)
    #     db.session.add(kind)
    # db.session.commit()

    # for item in range(1,20):
    #     title='course_%s' %item
    #     abstract='abstract'*random.randint(1,8)
    #     addtime=datetime.datetime.now()
    #     k_id=random.randint(1,9)
    #     course=Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    #     db.session.add(course)
    # db.session.commit()

    # for item in range(1,60):
    #     # title='title_%s' %item
    #     # time=random.randint(300,500)
    #     # hits=random.randint(100,200)
    #     # abstract='abstract'*random.randint(1,8)
    #     # t_id='teacher_%s' %random.randint(1,8)
    #     # c_id=random.randint(1,19)
    #     # video=Videos(title=title,time=time,hits=hits,abstract=abstract,t_id=t_id,c_id=c_id )

    #     db.session.add(video)
    # db.session.commit()
    # all_videos=db.session.query(Videos).order_by(Videos.id)
    # for item in all_videos:
    #     item.t_id=random.randint(1,8)

    return 'add success'

@main.route('/cour_commit',methods=["POST"])
def cour_commit():
    title=request.form['name']
    abstract=request.form['abstract']
    addtime=datetime.datetime.now()
    k_id=random.randint(1,9)
    course=Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    db.session.add(course)
    db.session.commit()
    return redirect("/course")

@main.route('/video_one/<courId>')
def video_one(courId):
    
    all_course=db.session.query(Courses).filter(Courses.id==courId).first()
    all_course=all_course.videos
    return render_template('video_one.html',all_videos=all_course)
    
@main.route('/video_delete',methods=['POST'])
def video_delete():
    id=request.form['id']
    video=db.session.query(model.Videos).filter(model.Videos.id==id).first()
    
    db.session.delete(video)
    db.session.commit()
    return 'success'

@main.route('/<id>')
def find(id):
    cour=db.session.query(Kinds).order_by(Kinds.id)
    kind=''
    if id=='course':
        kind=db.session.query(Kinds).first()
    else:
        kind=db.session.query(Kinds).filter(Kinds.id==id).first()
    course=kind.courses
    
    #如果加载js时，那么项目文件不能出现中文
    return render_template('course_one.html',courses=course,cour=cour)

@main.route('/logout')
def logout():

    return redirect(url_for('auth.login'))

@main.route('/cour_delete',methods=['POST'])
def cour_delete():
    id=request.form['id']
    course=db.session.query(Courses).filter(Courses.id==id).first()
    db.session.delete(course)
    db.session.commit()
    return 'success'
    
@main.route('/video_next_page/<num>')
def video_next_page(num):
    if num=='video':
        all_videos=db.session.query(Videos).offset(1).limit(16)
    else:
        num=(int(num)-1)*16
        all_videos=db.session.query(Videos).offset(num).limit(16)
    page=Videos.query.paginate(page=1,per_page=16,error_out=True)
    return render_template('video.html',all_videos=all_videos,page=page)